package com.snacks;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.snacks.data.DataFromHttpSever;
import com.snacks.data.URLconstan;

public class SearchDetail extends Activity implements OnClickListener{

	static final String ID = "id";
	static final String NAME = "name";
	static final String ADDRESS = "address";
	static final String CREATE_USER = "createuser";
	static final String CREATE_DATE = "createdate";
	static final String PHONE_NUMBER = "phonenumber";
	static final String THUMBNAIL = "thumbnail";

	ListView listView;
	CustomeAdapterSearch adapter;
	public ArrayList<HashMap<String, String>> mylist;
	private Button btBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_find);
		
		btBack = (Button) findViewById(R.id.btBack);
		btBack.setOnClickListener(this);
		
		// Lay ban tin intent
		Intent intentReceived = getIntent();
		Bundle mBundle = intentReceived.getExtras();

		// lay cac do vat trong bao
		String city = mBundle.getString("city");
		String district = mBundle.getString("district");
		String name = mBundle.getString("name");
		district = district.replace(" ", "_");
		district = district.trim();
		
		name = name.replace(" ", "_");
		name = name.trim();

		// rq chứa đối tượng reques để gửi lên web service
		ArrayList<NameValuePair> rq = new ArrayList<NameValuePair>();
		DataFromHttpSever dt = new DataFromHttpSever();
		String result = "";
		if(name.length() > 0){
			rq.add(new BasicNameValuePair("name", name));
			Log.i("log_tag", "Name: " + name);
			result = dt.getDataFromHttpSever(rq, URLconstan.getSearchItemByName());
		}
		else{
			rq.add(new BasicNameValuePair("city", city));
			rq.add(new BasicNameValuePair("district", district));
			Log.i("log_tag", "City: " + city + ", district: " + district);
			result = dt.getDataFromHttpSever(rq, URLconstan.getSearchItemService());
		}	

		mylist = new ArrayList<HashMap<String, String>>();

		try {
			JSONArray jArray = new JSONArray(result);	
			Log.i("log_tag", "Data Length: " + jArray.length());
			for (int i = 0; i < jArray.length(); i++) {

				HashMap<String, String> map = new HashMap<String, String>();
				JSONObject e = jArray.getJSONObject(i);

				Log.i("log_tag", "Name: " + e.getString("name") + ", address: "
						+ e.getString("address"));

				map.put(ID, e.getString("id"));
				map.put(NAME, e.getString("name"));
				map.put(ADDRESS,
						"Địa chỉ:" + e.getString("address") + ", "
								+ e.getString("area"));
				map.put(PHONE_NUMBER, "Mobile: " + e.getString("phonenumber"));
				map.put(THUMBNAIL,
						URLconstan.getServer() + "/uploads/"
								+ e.getString("thumbnail"));
				map.put(CREATE_USER, "Người Đăng: " + e.getString("createuser"));
				map.put(CREATE_DATE, "Ngày Đăng: " + e.getString("createdate"));

				mylist.add(map);
			}
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}
		
		try {
			listView = (ListView) findViewById(R.id.listview_find);

			adapter = new CustomeAdapterSearch(this, mylist);
			listView.setAdapter(adapter);

			// when click items
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					HashMap<String, String> hashmap = new HashMap<String, String>();
					hashmap = mylist.get(position);
					Intent intent = new Intent(getBaseContext(), ItemDetail.class);
					intent.putExtra("restaurant", hashmap);
					startActivity(intent);
				}
			});
		} catch (Exception e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btBack:
			finish();
			break;

		default:
			break;
		}
		
	}
}
